package curves;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import function.FunVariations;
import function.Function;
import function.Functions;
import function.SyntaxErrorException;

public class PanelCalcul extends JPanel{
	
	private static final long serialVersionUID = 1L;
	public JLabel y;
	private JComboBox funs_cb;
	

	public JComboBox getFunCb(){
		return funs_cb;
	}
	
	public PanelCalcul(final FunctionVariations var, final CurveFrame f){
		super();
		
		final CurveTracerUser ctu = f.getTracer();
		
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.setBorder(new EtchedBorder());
		
//		 ---- CHAMPS FONCTION -----
		JPanel fonction = new JPanel();
		
		//--COULEUR FONCTION
		JPanel function_color = new JPanel();
		final String[] color_choice_string = {"Noire", "Rouge", "Bleu", "Cyan", "Orange", "Vert", "Gris"};
		final Color[] color_choice = {Color.black, Color.red, Color.blue, Color.cyan, Color.orange, Color.green, Color.darkGray};
		final JComboBox fun_color_cb = new JComboBox(color_choice_string);
		function_color.add(new JLabel("Color : \n"));
		function_color.add(fun_color_cb);
		this.add(function_color);
		//--END
		
		final JTextField fonction_field = new JTextField(20);
		fonction_field.addActionListener(new ActionListener(){
			 public void actionPerformed(ActionEvent evt) {
				 	Function fun = null;
				 	try{
				 		fun = Functions.parse(fonction_field.getText());
				 		FunctionVariations fvar = (FunctionVariations)new FunVariations(fun, var.getXmin(), var.getXmax());
				 		fvar.tabulate(f.getControls().currentPrecision());
				 		CurveTracerUser ctu = f.getTracer();
				 		ctu.addFunctionVariations(fvar, fonction_field.getText(), color_choice[fun_color_cb.getSelectedIndex()]);

				 		funs_cb.addItem(fonction_field.getText());
				 		funs_cb.repaint();
				 		f.repaint();
				 	} catch (SyntaxErrorException e){
				 		fonction_field.setText("Fonction invalide !");
				 		fonction_field.selectAll();
				 	} catch (IOException e){
				 		e.printStackTrace();
				 	}
			    }
		});
		
		
		fonction.add(new JLabel("Fonction :\n"));
		fonction.add(fonction_field);
		this.add(fonction);
//		 ---- END CHAMPS FONCTION ----
		
		
//		 ---- CHAMPS CALCUL ----
		
		//-- Fonctions
		JPanel funs_panel = new JPanel();
		funs_cb = new JComboBox(ctu.getFunVarNames());
		funs_panel.add(new JLabel("Fonctions : \n"));
		funs_panel.add(funs_cb);
		
		JButton suppression = new JButton("Supprimer");
		suppression.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(funs_cb.getSelectedIndex() >= 0){
					ctu.removeFunVar(funs_cb.getSelectedIndex());
					funs_cb.removeItemAt(funs_cb.getSelectedIndex());
					f.repaint();
				}
				
			}
		}
		);
		
		funs_panel.add(suppression);
		
		this.add(funs_panel);
		
		JPanel calculX = new JPanel();
		calculX.setLayout(new FlowLayout());
		JLabel labelX = new JLabel("Calcul de Y pour X =");
		JLabel labelY = new JLabel("=> Y = ");
		JTextField textX = new JTextField(4);
		y = new JLabel();
		CaretListener updateY = new CaretListener(){
				public void caretUpdate(CaretEvent e) {
					JTextField text = (JTextField)e.getSource();
					double xvar;
					try {
						xvar = Double.parseDouble(text.getText());
					} catch(NumberFormatException e2){
						xvar = 0.0;
					}
					
					FunVariations fvar = (FunVariations) ctu.getFunctionVariations(funs_cb.getSelectedIndex());
					y.setText(Double.toString(fvar.fun(xvar)));
				}
			};
		textX.addCaretListener(updateY);
	
		calculX.add(labelX);
		calculX.add(textX);
		calculX.add(labelY);
		calculX.add(y);
		this.add(calculX);
//		 ---- END CHAMPS CALCUL ----
		
		
		//Mise à jour de l'infos "sum" (intégrale) lors de la selection d'une fonction dans la ComboBox
		/*
		ActionListener cbListener = new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(funs_cb.getSelectedIndex() != -1){
					f.infos.integral.setText("sum = " + ctu.getFunctionVariations(funs_cb.getSelectedIndex()).getIntegral());
					f.infos.repaint();
				}
			}
		};
		*/
		//funs_cb.addActionListener(cbListener);
	}

}
